package com.peanut.baby.mvp.bbs.detail;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.BBSComment;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.model.MomentRel;
import com.peanut.baby.model.ShareType;
import com.peanut.baby.model.VarPoint;
import com.tencent.mars.xlog.Log;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;


/**
 * Created by anonymouslystudio on 2017/8/21.
 */

public class BBSMomentDetailPresenter implements BBSMomentDetailContract.Presenter{
    private static final String TAG = BBSMomentDetailPresenter.class.getSimpleName();
    private BBSMomentDetailContract.View view;
    private Activity            activity;
    public BBSMomentDetailPresenter(BBSMomentDetailContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getMomentDetail(String id) {
        Observable obs = RetrofitClient.getInstance().getMomentDetail(id,InitManager.getInstance().getUserIdOrZero());
        obs.compose(BaseScheduler.<BBSMoment>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<BBSMoment>() {
            @Override
            protected void onHandleSuccess(BBSMoment o) {
                view.onDetailGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetDetailFailed(msg);
            }
        });
    }

    @Override
    public void getComments(int pageNo, int pageSize, String momentId) {
        Observable obs = RetrofitClient.getInstance().getMomentComments(momentId,pageNo,pageSize);
        obs.compose(BaseScheduler.<List<BBSComment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<BBSComment>>() {
            @Override
            protected void onHandleSuccess(List<BBSComment> o) {
                view.onCommentsGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetCommentsFailed(msg);
            }
        });
    }

    @Override
    public void sendComment(String momentId, int type, String title, String content) {
        Observable obs = RetrofitClient.getInstance().sendBBSComment(momentId, InitManager.getInstance().getUserId(),
                type,title,content,InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<BBSComment>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<BBSComment>() {
            @Override
            protected void onHandleSuccess(BBSComment o) {
                view.onSendCommentSuccess(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onSendCommentFailed(msg);
            }
        });
    }

    @Override
    public void sharePoint(String momentId) {
        Observable obs = RetrofitClient.getInstance().sharePoint(InitManager.getInstance().getUserId(),
                ShareType.TYPE_SHARE_MOMENT+"",momentId);
        obs.compose(BaseScheduler.<VarPoint>compose()).compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<VarPoint>() {
            @Override
            protected void onHandleSuccess(VarPoint o) {
                InitManager.getInstance().getUser().userPoint = o.totalPoint;
                InitManager.getInstance().updateLocalUser();
                view.onSharePointSuccess("花生壳加"+o.variedPoint+",总花生壳"+o.totalPoint);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                Log.w(TAG,"sharePoint failed "+msg);
            }
        });
    }


    @Override
    public void getPostRel(String id) {
        Observable obs = RetrofitClient.getInstance().getPostRel(id,InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<BaseResponse<MomentRel>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<MomentRel>() {
                    @Override
                    protected void onHandleSuccess(MomentRel rel) {
                        view.onPostRelGet(true,rel,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onPostRelGet(false,null,msg);
                    }

                });
    }
}
